package com.pro.system.Controller;

import com.pro.system.Repository.MovieRepository;
import com.pro.system.entity.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("admin/movies")
public class MovieController {

    @Autowired
    private MovieRepository movieRepository;

    @GetMapping
    public String showMovie (Model model){
        List<Movie> movie = movieRepository.findAll();
        model.addAttribute("movie",movie);
        return "web/background/movies/movies";
    }


    @GetMapping("intonew")
    public String intonew (Model model){
        Movie movie = new Movie();
        model.addAttribute(movie);
        return "web/background/movies/movies-new";
    }
    @GetMapping("intodelete")
    public String intodelete (Model model){
        Movie movie = new Movie();
        model.addAttribute(movie);
        return "web/background/movies/movies-delete";
    }

    @PostMapping("save")
    public String save(Model model,Movie movie){
        Movie save = movieRepository.save(movie);
        if(save != null){
            return "web/background/movies/movies-new";
        }
        else {
            return "";
        }
    }

    @PostMapping("delete")
    public String delete(Model model, Movie movie){
        Movie movie1 = new Movie();
        model.addAttribute(movie1);
        movieRepository.deleteById(movie.getMovieId());
        return "web/background/movies/movies-delete";
    }

//    @PostMapping("/research")
//    public String research(Model model,Movie movie){
//        List<Movie> movieByNameLike = movieRepository.findMovieByNameLike(movie.getName());
//        model.addAttribute("movie",movieByNameLike);
//        return  "web/movies";
//    }

}
